/* --------------------------------------------------------
 * Copyright (c) aeky , Inc.  All rights reserved.
 * --------------------------------------------------------
 */
package com.aeky.client.view.ib5;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.aeky.client.common.AppParams;
import com.aeky.client.common.DefaultCallback;
import com.aeky.client.rpcservice.UserRPCService;
import com.aeky.client.rpcservice.UserRPCServiceAsync;
import com.aeky.client.ui.AekyListGridRecord;
import com.aeky.client.view.base.BaseCenterBodyPanel;
import com.aeky.model.BaseMapData;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;

/**
 * 
 * @author aeky
 */
public class IB5 extends BaseCenterBodyPanel {

    private static UserRPCServiceAsync userService = AppParams.getService(UserRPCService.class);

    private IButton searchBtn;

    private ListGrid userGrid;

    public IB5(String title) {
        super(title);
        instanceWidgets();
        ininWidgetListener();
        this.addMember(searchBtn);
        this.addMember(userGrid);
    }

    private void instanceWidgets() {
        searchBtn = new IButton("查询");
        userGrid = new ListGrid();
        userGrid.setWidth(600);
        userGrid.setHeight(400);
        ListGridField nameField = new ListGridField("loginName", "用户名");
        ListGridField passField = new ListGridField("password", "密码");
        userGrid.setFields(nameField, passField);
        userGrid.setCanResizeFields(true);
    }

    private void ininWidgetListener() {
        searchBtn.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                userService.findAllUser(new DefaultCallback<BaseMapData>() {
                    public void onSuccess(BaseMapData data) {
                        super.onSuccess(data);
                        List<ListGridRecord> list = new ArrayList<ListGridRecord>();
                        List<Map<String, Object>> mapList = (List<Map<String, Object>>) data.get("userMapList");
                        AekyListGridRecord algr = null;
                        for (Map<String, Object> map : mapList) {
                            algr = new AekyListGridRecord(map);
                            list.add(algr);
                        }
                        userGrid.setData(list.toArray(new ListGridRecord[0]));
                    }
                });
            }
        });
    }

}
